﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using vJine.Lua.GUI.BLL;

namespace vJine.Lua.GUI {
    /// <summary>
    /// WinOptions.xaml 的交互逻辑
    /// </summary>
    public partial class WinOptions : Window {
        public WinOptions() {
            InitializeComponent();

            this.MinWidth = this.Width;
            this.MinHeight = this.Height;
        }

        public static void ShowOptions(AppGUI appGUI, WinFlow winFlow, LuaContext luaContext, NodeConfig nConfig, bool show_itb = false, string caption = "", bool is_degin = true) {
            WinOptions winOptions =
                new WinOptions() {
                    ShowInTaskbar = show_itb,
                    Title = string.IsNullOrEmpty(caption) ? string.Format("[{0}]@[{1}]", nConfig.alias, nConfig.uri) : caption,
                    DataContext = nConfig
                };
            bool is_running = false;
            Action<NodeConfig> on_begin = (NodeConfig cmd) => {
                lock (winOptions) {
                    is_running = true;
                }
            };
            Action<NodeConfig> on_finish = (NodeConfig cmd) => {
                lock (winOptions) {
                    is_running = false;
                }
            };
            nConfig.on_begin += on_begin;
            nConfig.on_finish += on_finish;
            winOptions.Closing += (object sender, CancelEventArgs e) => {
                lock (winOptions) {
                    e.Cancel = is_running;
                    if (is_running) {
                        return;
                    }

                    nConfig.on_begin -= on_begin;
                    nConfig.on_finish -= on_finish;
                }
            };

            winOptions.nodeStatus.set_view_status(is_degin);
            winOptions.nodeStatus.set_bound(appGUI, luaContext, nConfig,
                (string msg, string title, MessageBoxButton button, MessageBoxImage icon) => {
                    MessageBox.Show(winOptions, msg, title, button, icon);
                });

            winOptions.Top = 0;
            winOptions.Height = SystemParameters.FullPrimaryScreenHeight;

            try {
                winOptions.ShowDialog();
            } finally {
                winFlow.set_bound();
                winOptions = null;
            }
        }
    }
}
